// pages/allRoom/allRoom.js

let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ad_list: [], //顶部轮播图
    allRoomList: "", //房间列表
    changeList: "", //类型列表
    changeNum: 0, //类型下标
    iskuolie: true,
    page_loading:false,

    category: [], //选择
    ad_list: [], //轮播图
    room_list: [],
    page:1,
    jiazaizhong:false,
    hasMore:true,

    height_swiper: '',
  },
  height_swipers() {
    let that = this;
    var query = wx.createSelectorQuery();
    query.select('.timu_list'+that.data.changeNum).boundingClientRect(function (rect) {
      that.setData({
        // 获取要循环标签的高度 
        height_swiper: rect.height,
      })
    }).exec();
  },
  toroom(data) {
    wx.navigateTo({
      url: '/pages/room/room?roomId=' + data.currentTarget.dataset.room_id,
    })
  },
  back_index() {
    wx.navigateBack({ //返回
      delta: 1
    })
  },
  random_room() {
    let params = {
      cid: this.data.changeNum,
    };
    axios.axios({
      url: "/chat/quickIn",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        console.log(res.data.result);
        wx.navigateTo({
          url: '/pages/room/room?roomId=' + res.data.result.room_id,
        })
      } else {
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 2000 //持续的时间
        })
      }
    })
  },
  switchTab(e){
  this.setData({
    changeNum:e.detail.current
  })
  this.indexV3(e.detail.current)
  },
  indexV3(data) {
    this.setData({
      hasMore:true,
      jiazaizhong:false,
      page_loading:true,
      page:1,
      changeNum: data.currentTarget ? data.currentTarget.dataset.cid : data,
    })
    let params = { 
      cid: data.currentTarget ? data.currentTarget.dataset.cid : data,
      page: this.data.page,
    };
    axios.axios({
      url: "/index/roomListV2",
      data: params
    }).then((res) => {
      this.setData({
        page_loading:false
      })
      if (res.data.status == 0) {
        this.setData({
        
          category: res.data.result.category, //选择
          ad_list: res.data.result.ad_list, //轮播图
          room_list: res.data.result.room_list, //推荐房间
        })
        this.height_swipers()
      }
 
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    options.cid?this.indexV3(options.cid):this.indexV3(0)
  },

  preview(data) {
    let url = data.currentTarget.dataset.url;
    wx.navigateTo({
      url: '../link/link?url=' + url
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  drop_down_gangup() {
    let _ = this;
    var p;
    _.data.hasMore?p=++_.data.page:p=_.data.page
    let params = {
      cid: _.data.changeNum,
      page: p,
    };
    axios.axios({
      url: "/index/roomListV2",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        if (res.data.result.room_list.length != 0) {
          _.setData({
            page: p,
            room_list: _.data.room_list.concat(res.data.result.room_list),
          })
          _.height_swipers()
        } else {
          _.setData({
            page: p,
            hasMore: false
          })
          let timer
          clearTimeout(timer)
          timer = setTimeout(() => { //设置延迟执行
            _.setData({
              jiazaizhong: false,
            })
          }, 3000);
        }
      } else {
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 2000 //持续的时间
        })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      jiazaizhong:true,
    })
    this.drop_down_gangup()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
